package casanet.parser;

import java.util.Hashtable;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ParserHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean in_listeArticles = false;
	private boolean in_article = false;
	private boolean in_titre = false;
	private boolean in_idArticle = false;
	private boolean in_freindUrl = false;
	private boolean in_imageP = false;
	private boolean in_imageM = false;
	private boolean in_imageG = false;
	private boolean in_description = false;
	private boolean in_auteur = false;
	private boolean in_source = false;
	private boolean in_date = false;
	private boolean in_lieu = false;
	private boolean in_corpstext = false;
	private Hashtable<String, ParserArticle> dic = new Hashtable<String, ParserArticle>();
	private Vector<String> key = new Vector<String>();
	private ParserArticle myParsedExampleDataSet = new ParserArticle();

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public Hashtable<String, ParserArticle> getDic() {
		return dic;
	}

	public void setDic(Hashtable<String, ParserArticle> dic) {
		this.dic = dic;
	}

	public Vector<String> getKey() {
		return key;
	}

	public void setKey(Vector<String> key) {
		this.key = key;
	}
	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedExampleDataSet = new ParserArticle();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {			
		if (qName.equalsIgnoreCase("listeArticles")) {
			this.in_listeArticles = true;			
		}
		if (qName.equalsIgnoreCase("article")) {
			this.in_article = true;			
		}if (qName.equalsIgnoreCase("titre")) {
			this.in_titre = true;					
		}else if (qName.equalsIgnoreCase("idArticle")) {
			this.in_idArticle = true;			
		}else if (qName.equalsIgnoreCase("friendURL")) {
			this.in_freindUrl = true;
		}else if (qName.equalsIgnoreCase("imageP")) {
			this.in_imageP = true;			
		}else if (qName.equalsIgnoreCase("imageM")) {
			this.in_imageM = true;
		}else if (qName.equalsIgnoreCase("imageG")) {
			this.in_imageG = true;		
		}else if (qName.equalsIgnoreCase("description")) {
			this.in_description = true;		
		}else if (qName.equalsIgnoreCase("auteur")) {
			this.in_auteur = true;		
		}else if (qName.equalsIgnoreCase("source")) {
			this.in_source = true;		
		}else if (qName.equalsIgnoreCase("date")) {
			this.in_date = true;		
		}else if (qName.equalsIgnoreCase("lieu")) {		
			this.in_lieu = true;		
		}else if (qName.equalsIgnoreCase("corpstext")) {
			this.in_corpstext = true;	
		}
	}		

	/** Gets be called on closing tags like: 
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException {

	}

	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */
	@Override
	public void characters(char ch[], int start, int length) {

		if (this.in_listeArticles) {
			this.in_listeArticles = false;
		}
		else if (this.in_article) {
			this.in_article = false;
		}
		else if (this.in_titre) {			
			myParsedExampleDataSet.setTitre(new String(ch, start, length));
			//System.out.println("\tTitre : \n"+new String(ch,start,length)+"\n");
			this.in_titre = false;
		}
		else if (this.in_idArticle) {
			myParsedExampleDataSet.setIdArticle(new String(ch, start, length));
			//System.out.println("\t idArticle : \n"+new String(ch,start,length)+"\n");				
			this.in_idArticle = false;
		}
		else if (this.in_freindUrl) {
			myParsedExampleDataSet.setFreindUrl(new String(ch, start, length));
			//System.out.println("\t in_freindUrl"+new String(ch,start,length)+"\n");
			this.in_freindUrl = false;
		}
		else if (this.in_imageP) {
			myParsedExampleDataSet.setImageP(new String(ch, start, length));
			//System.out.println("\t in_imageP : \n"+new String(ch,start,length)+"\n");
			this.in_imageP = false;
		}		
		else if (this.in_imageM) {
			myParsedExampleDataSet.setImageM(new String(ch, start, length));
			//System.out.println("\t in_imageM : \n"+new String(ch,start,length)+"\n");
			this.in_imageM = false;
		}
		else if (this.in_imageG) {
			myParsedExampleDataSet.setImageG(new String(ch, start, length));
			//System.out.println("\t in_imageG : \n"+new String(ch,start,length)+"\n");
			this.in_imageG = false;
		}
		else if (this.in_description) {
			myParsedExampleDataSet.setDescription(new String(ch, start, length));
			//System.out.println("\t in_description : \n"+new String(ch,start,length)+"\n");
			this.in_description = false;
		}
		else if (this.in_auteur) {
			myParsedExampleDataSet.setAuteur(new String(ch, start, length));
			//System.out.println("\t in_auteur : \n"+new String(ch,start,length)+"\n");
			this.in_auteur = false;
		}		
		else if (this.in_source) {
			myParsedExampleDataSet.setSource(new String(ch, start, length));
			//System.out.println("\t in_source \n"+new String(ch,start,length)+"\n");
			this.in_source = false;
		}				
		else if (this.in_date) {
			myParsedExampleDataSet.setDate(new String(ch, start, length));
			//System.out.println("\t in_date \n"+new String(ch,start,length)+"\n");
			this.in_date = false;
		}						
		else if (this.in_lieu) {
			myParsedExampleDataSet.setLieu(new String(ch, start, length));
			//System.out.println("\t in_lieu \n"+new String(ch,start,length)+"\n");
			this.in_lieu = false;
		}						
		else if (this.in_corpstext ) {		
			myParsedExampleDataSet.setCorpstext(new String(ch, start, length));
			//System.out.println("\t in_corpstext \n"+new String(ch,start,length)+"\n");
			this.in_corpstext = false;
			this.key.add(myParsedExampleDataSet.getIdArticle());
			dic.put(this.myParsedExampleDataSet.getIdArticle(), this.myParsedExampleDataSet);
			this.myParsedExampleDataSet = new ParserArticle();
			//System.out.println("-------------------------------");
		}					
	}


}